<?php
function idx2jis($idx,$basediff=0){
	$starts = array(
		0x2100,/* empty */
		0x2120, 0x2220, 0x2320, 0x2420, 0x2520, 0x2620, 0x2720, 0x2820,
		0x2160, 0x2260, 0x2360, 0x2460, 0x2560, 0x2660, 0x2760, 0x2860,
		0x2140, 0x2240, 0x2340, 0x2440, 0x2540, 0x2640, 0x2740, 0x2840,
		0x2940, 0x2960, 0x2980, 0x29a0, 0x29c0, 0x29e0, 0x2a00,/* whole line empty */
		0x3020, 0x3120, 0x3220, 0x3320, 0x3420, 0x3520, 0x3620, 0x3720, 0x3820, 0x3920, 0x3a20, 0x3b20, 0x3c20, 0x3d20, 0x3e20, 0x3f20,
		0x3040, 0x3140, 0x3240, 0x3340, 0x3440, 0x3540, 0x3640, 0x3740, 0x3840, 0x3940, 0x3a40, 0x3b40, 0x3c40, 0x3d40, 0x3e40, 0x3f40,
		0x3060, 0x3160, 0x3260, 0x3360, 0x3460, 0x3560, 0x3660, 0x3760, 0x3860, 0x3960, 0x3a60, 0x3b60, 0x3c60, 0x3d60, 0x3e60, 0x3f60,
		0x4020, 0x4120, 0x4220, 0x4320, 0x4420, 0x4520, 0x4620, 0x4720, 0x4820, 0x4920, 0x4a20, 0x4b20, 0x4c20, 0x4d20, 0x4e20, 0x4f20,
		0x4040, 0x4140, 0x4240, 0x4340, 0x4440, 0x4540, 0x4640, 0x4740, 0x4840, 0x4940, 0x4a40, 0x4b40, 0x4c40, 0x4d40, 0x4e40, 0x4f40,
		0x4060, 0x4160, 0x4260, 0x4360, 0x4460, 0x4560, 0x4660, 0x4760, 0x4860, 0x4960, 0x4a60, 0x4b60, 0x4c60, 0x4d60, 0x4e60, 0x4f60,
		0x5020, 0x5120, 0x5220, 0x5320, 0x5420, 0x5520, 0x5620, 0x5720, 0x5820, 0x5920, 0x5a20, 0x5b20, 0x5c20, 0x5d20, 0x5e20, 0x5f20,
		0x5040, 0x5140, 0x5240, 0x5340, 0x5440, 0x5540, 0x5640, 0x5740, 0x5840, 0x5940, 0x5a40, 0x5b40, 0x5c40, 0x5d40, 0x5e40, 0x5f40,
		0x5060, 0x5160, 0x5260, 0x5360, 0x5460, 0x5560, 0x5660, 0x5760, 0x5860, 0x5960, 0x5a60, 0x5b60, 0x5c60, 0x5d60, 0x5e60, 0x5f60,
		0x6020, 0x6120, 0x6220, 0x6320, 0x6420, 0x6520, 0x6620, 0x6720, 0x6820, 0x6920, 0x6a20, 0x6b20, 0x6c20, 0x6d20, 0x6e20, 0x6f20,
		0x6040, 0x6140, 0x6240, 0x6340, 0x6440, 0x6540, 0x6640, 0x6740, 0x6840, 0x6940, 0x6a40, 0x6b40, 0x6c40, 0x6d40, 0x6e40, 0x6f40,
		0x6060, 0x6160, 0x6260, 0x6360, 0x6460, 0x6560, 0x6660, 0x6760, 0x6860, 0x6960, 0x6a60, 0x6b60, 0x6c60, 0x6d60, 0x6e60, 0x6f60,
		0x7020, 0x7120, 0x7220, 0x7320, 0x7420, 0x7520 /* empty */, 0x7620 /* empty */, 0x7720 /* empty */,
		0x7060, 0x7160, 0x7260, 0x7360, 0x7460 /* empty */, 0x7560 /* empty */, 0x7660 /* empty */, 0x7760 /* empty */,
		0x7040, 0x7140, 0x7240, 0x7340, 0x7440 /* empty */, 0x7540 /* empty */, 0x7640 /* empty */, 0x7740 /* empty */,
		0x7880, 0x78a0, 0x78c0, 0x78e0, 0x7900, 0x7920, 0x7940, 0x7960 /* whole line empty */
		);
	$i=$idx-$basediff;
	$s=intval($i/32);
	return $starts[$s]+($i%32);
//return $idx;
}

function BitReverse($bData) {
	$lookup = array( 0, 8,  4, 12,
                     2, 10, 6, 14,
                     1, 9,  5, 13,
                     3, 11, 7, 15 );
	return (($lookup[($bData & 0x0F)]) << 4) + $lookup[(($bData & 0xF0) >> 4)];
}

$handle = fopen(@$_SERVER['argv'][1],'rb') or die('Cannot open file.');
$fsize = filesize(@$_SERVER['argv'][1]);
$contents = fread($handle, $fsize);
$byteArray = unpack("C*",$contents); $tmp=array_shift($byteArray); array_unshift($byteArray,$tmp);
//print_r($byteArray);

$w=@$_SERVER['argv'][2] ? @$_SERVER['argv'][2] : 8;
$h=@$_SERVER['argv'][3] ? @$_SERVER['argv'][3] : 16;
$offset=@$_SERVER['argv'][4] ? @$_SERVER['argv'][4] : 0;
$skip=@$_SERVER['argv'][5] ? @$_SERVER['argv'][5] : 0;
$chrs=@$_SERVER['argv'][6] ? @$_SERVER['argv'][6] : 256;

$fp=fopen(str_replace('.','-',@$_SERVER['argv'][1]).'.bdf','wb') or die('Cannot write file');
$fa=ceil($h*0.75);
$fd=$h-$fa;
$hdr= <<<HEADER
STARTFONT 2.1
FONT ${w}x$h
SIZE $h 75 75
FONTBOUNDINGBOX $w $h 0 -$fd
STARTPROPERTIES 6
FONT_ASCENT $fa
FONT_DESCENT $fd
SPACING "C"
DEFAULT_CHAR 32
CHARSET_REGISTRY "JISX0208.1978"
CHARSET_ENCODING "0"
ENDPROPERTIES
CHARS $chrs

HEADER;
fwrite($fp,$hdr);

$swidth = $w*90;
$bwidth=ceil($w/8);
for ($char=0; $char<$chrs; $char++)
{
	$chrcode=idx2jis(intval($char));
	fwrite($fp,"STARTCHAR ".dechex($chrcode)."\n");
	fwrite($fp,"ENCODING ".($chrcode)."\n");
	fwrite($fp,"COMMENT CHRIDX ".intval($char)."\n");
	fwrite($fp,"SWIDTH $swidth 0\n");
	fwrite($fp,"DWIDTH $w 0\n");
	fwrite($fp,"BBX $w $h 0 -$fd\n");
	fwrite($fp,"BITMAP\n");

	for ($y=0; $y<$h*$bwidth/*-1*/; $y++)
	{
		$v='';
		//printf("offset=%d, chr=%d\n",$char*($bwidth*$h)+$y,$byteArray[$char*($bwidth*$h)+$y]);
		for($j=1;$j<$bwidth;$j++)
			$v.=sprintf("%02X",BitReverse($byteArray[$offset+$char*$skip+$char*($bwidth*$h)+$y++]));
		$v.=sprintf("%02X\n",BitReverse($byteArray[$offset+$char*$skip+$char*($bwidth*$h)+$y]));
		fwrite($fp, $v);
	}
//		fwrite($fp, "00\n");

	fwrite($fp,"ENDCHAR\n");
}

fwrite($fp,"ENDFONT\n");
fclose($fp);
